package com.habdqn.easybuy.web.manage.product.category;

import com.habdqn.easybuy.entity.ProductCategory;
import com.habdqn.easybuy.service.ProductCategoryService;
import com.habdqn.easybuy.service.impl.ProductCategoryServiceImpl;
import com.habdqn.easybuy.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Xing.Jiejian
 * @date 2020/11/28
 */
@WebServlet(name = "ProductCategoryUpdateServlet", urlPatterns = "/manage/product/category/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId = request.getParameter("id");
        if (StringUtils.isNotBlank(strId)) {
            int id = Integer.parseInt(strId);
            // 先获取原用户信息
            ProductCategoryService productCategoryService = new ProductCategoryServiceImpl();
            ProductCategory productCategory = productCategoryService.queryById(id);
            // 更新表单数据
            productCategory.setName(request.getParameter("name"));
            String strParentId = request.getParameter("parentId");
            if (StringUtils.isNotBlank(strParentId)) {
                productCategory.setParentId(Integer.parseInt(strParentId));
            }


            // 执行更新业务
            if (productCategoryService.update(productCategory) != null) {
                response.setContentType("text/html;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.println("<script>");
                out.println("alert('修改分类成功！');");
                out.println("location.href='" + request.getContextPath() + "/manage/product/category';");
                out.println("</script>");
            } else {
                request.setAttribute("errorMsg", "修改失败");
                request.setAttribute("productCategory", productCategory);
                request.getRequestDispatcher("/WEB-INF/manage/product/category-modify.jsp").forward(request, response);
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId = request.getParameter("id");
        if (StringUtils.isNotBlank(strId)) {
            int id = Integer.parseInt(strId);
            ProductCategoryService productCategoryService = new ProductCategoryServiceImpl();
            // 先获取原用分类信息
            ProductCategory productCategory = productCategoryService.queryById(id);
            // 获取顶级分类
            List<ProductCategory> categories = productCategoryService.queryByParentId(0);
            request.setAttribute("productCategory", productCategory);
            request.setAttribute("categories",categories);
            request.getRequestDispatcher("/WEB-INF/manage/product/category-modify.jsp").forward(request, response);
        } else {
            // 未传入id则直接进入列表页
            response.sendRedirect(request.getContextPath() + "/manage/product/category");
        }
    }
}
